package com.zzh.androidsocket;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zzh.socketlibrary.manager.ISocketManager;
import com.zzh.socketlibrary.manager.MessageSocketManager;
import com.zzh.socketlibrary.manager.SocketDataConst;
import com.zzh.socketlibrary.manager.SocketManager;

import java.io.UnsupportedEncodingException;

public class MainActivity extends AppCompatActivity implements ISocketManager.Host {
    private String TAG = MainActivity.class.getSimpleName();

    TextView tvMsg;
    Button btnSend;
    EditText editSendMsg;

    MessageSocketManager socketManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiView();
        initSocketManager();
    }

    private void intiView() {
        tvMsg = findViewById(R.id.tvReceiveMsg);
        btnSend = findViewById(R.id.btnSend);
        editSendMsg = findViewById(R.id.editSendMsg);
        tvMsg.setText(getString(R.string.receive_msg, ""));
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editSendMsg.getText() != null && !TextUtils.isEmpty(editSendMsg.getText().toString())) {
                    try {
                        byte[] sendMsg = (editSendMsg.getText().toString()).getBytes("utf-8");
                        socketManager.sendData(sendMsg);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                } else
                    Toast.makeText(MainActivity.this, "请输入要发送的信息", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initSocketManager() {
        socketManager = new MessageSocketManager();
        socketManager.init();
        socketManager.setHost(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (socketManager != null)
            socketManager.connect();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (socketManager != null)
            socketManager.disconnect();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (socketManager != null)
            socketManager.destroy();
    }

    @Override
    public void onReceiveData(byte[] receiveMsg) {
        Log.i(TAG, "onReceiveData");
        try {
            String receive = new String(receiveMsg, "utf-8");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvMsg.setText(getString(R.string.receive_msg, receive));
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onConnectSuccess() {
        Log.i(TAG, "onConnectSuccess");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
            }
        });
    }
}